package com.china08.yxyapi.service.manage.tec.stuattendance.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import com.china08.yxyapi.entity.mg.attendance.YxyStuAttendance;
import com.china08.yxyapi.service.manage.tec.stuattendance.YxyStuAttendanceService;

/**
 * 晨检到班数据处理
 * 
 */
@Service
public class YxyStuAttendanceServiceImpl implements YxyStuAttendanceService {

    @Autowired
    public MongoOperations mongoOperations;

    /**
     * 查看记录是所需数据
     * 
     */
    @Override
    public List<Object> getStuAttendance(String schoolId, String classId, Integer month, String signClass,
            String keyWord, Integer pageNum, Integer pageSize) {

        Integer skip = pageNum * pageSize;
        Criteria criteria = new Criteria();
        // 学校id
        criteria.and("schoolId").is(schoolId);
        // 班级id
        criteria.and("classId").is(classId);
        // 月份数据
        criteria.and("monthDate").is(month);
        // 到班状态
        if (StringUtils.isNotBlank(signClass)) {
            if (signClass.equals("1")) {
                // 已到班
                criteria.and("sign_class").is(Integer.parseInt(signClass));
            } else if (signClass.equals("0")) {
                // 未到班
                criteria.and("sign_class").is(Integer.parseInt(signClass));
            }
        }
        // 输入参数 (在学生姓名&学号模糊)
        if (StringUtils.isNotBlank(keyWord)) {
            if (!IntegerUtil(keyWord)) {
                criteria.andOperator(Criteria.where("studentName").regex(".*?" + keyWord + ".*"));
            } else {
                criteria.orOperator(Criteria.where("studentNo").is(Long.valueOf(keyWord)),
                        Criteria.where("studentName").regex(".*?" + keyWord + ".*"));
            }
        }

        Query query = new Query();
        query.addCriteria(criteria);
        query.with(new Sort(new Order(Direction.DESC, "dayDate")));

        query.skip(skip);
        query.limit(pageSize);
        List<YxyStuAttendance> list = mongoOperations.find(query, YxyStuAttendance.class);

        List<Object> rerutnList = new ArrayList<Object>();

        for (YxyStuAttendance ysa : list) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("index", (skip + new Integer(list.indexOf(ysa) + 1)));
            map.put("id", ysa.getId());
            String dateChang = ysa.getDayDate().toString();
            map.put("date", dateChang.substring(0, 4) + "-" + dateChang.substring(4, 6) + "-" + dateChang.substring(6));
            map.put("stuName", ysa.getStudentName());
            map.put("stuNo", ysa.getStudentNo() == null ? null : ysa.getStudentNo());
            map.put("className", ysa.getClassName());
            map.put("signInDate",
                    ysa.getSignInDate() == null ? null
                            : ysa.getSignInDate().getHours() + ":" + (ysa.getSignInDate().getMinutes() < 10
                                    ? "0" + ysa.getSignInDate().getMinutes() : ysa.getSignInDate().getMinutes()));
            map.put("signOutDate",
                    ysa.getSignOutDate() == null ? null
                            : ysa.getSignOutDate().getHours() + ":" + (ysa.getSignOutDate().getMinutes() < 10
                                    ? "0" + ysa.getSignOutDate().getMinutes() : ysa.getSignOutDate().getMinutes()));
            map.put("inspection", ysa.getInspection());
            map.put("inspectionDate",
                    ysa.getInspectionDate() == null ? null
                            : ysa.getInspectionDate().getHours() + ":"
                                    + (ysa.getInspectionDate().getMinutes() < 10
                                            ? "0" + ysa.getInspectionDate().getMinutes()
                                            : ysa.getInspectionDate().getMinutes()));
            map.put("signClass", ysa.getSignClass() == 0 ? "未到班" : "已到班");
            map.put("signClassDate",
                    ysa.getSignClassDate() == null ? null
                            : ysa.getSignClassDate().getHours() + ":"
                                    + (ysa.getSignClassDate().getMinutes() < 10
                                            ? "0" + ysa.getSignClassDate().getMinutes()
                                            : ysa.getSignClassDate().getMinutes()));
            rerutnList.add(map);
        }
        return rerutnList;
    }

    /**
     * 查看记录是所需数据数量
     * 
     */
    @Override
    public Long getStuAttendanceCount(String schoolId, String classId, Integer month, String signClass, String keyWord,
            Integer pageNum, Integer pageSize) {
        Criteria criteria = new Criteria();
        // 学校id
        criteria.and("schoolId").is(schoolId);
        // 班级id
        criteria.and("classId").is(classId);
        // 月份数据
        criteria.and("monthDate").is(month);
        // 到班状态
        if (StringUtils.isNotBlank(signClass)) {
            if (signClass.equals("1")) {
                // 已到班
                criteria.and("sign_class").is(Integer.parseInt(signClass));
            } else if (signClass.equals("0")) {
                // 未到班
                criteria.and("sign_class").is(Integer.parseInt(signClass));
            }
        }
        // 输入参数 (在学生姓名&学号模糊)
        if (StringUtils.isNotBlank(keyWord)) {
            if (!IntegerUtil(keyWord)) {
                criteria.andOperator(Criteria.where("studentName").regex(".*?" + keyWord + ".*"));
            } else {
                criteria.orOperator(Criteria.where("studentNo").is(Long.valueOf(keyWord)),
                        Criteria.where("studentName").regex(".*?" + keyWord + ".*"));
            }
        }

        Query query = new Query();
        query.addCriteria(criteria);

        long count = mongoOperations.count(query, YxyStuAttendance.class);
        return count;
    }

    /**
     * 导出是所需数据
     * 
     */
    @Override
    public List<Map<String, String>> getStuAttendanceForDownload(String schoolId, String classId, Integer month,
            String signClass, String keyWord) {
        Criteria criteria = new Criteria();
        // 学校id
        criteria.and("schoolId").is(schoolId);
        // 班级id
        criteria.and("classId").is(classId);
        // 月份数据
        criteria.and("monthDate").is(month);
        // 到班状态
        if (StringUtils.isNotBlank(signClass)) {
            if (signClass.equals("1")) {
                // 已到班
                criteria.and("sign_class").is(Integer.parseInt(signClass));
            } else if (signClass.equals("0")) {
                // 未到班
                criteria.and("sign_class").is(Integer.parseInt(signClass));
            }
        }
        // 输入参数 (在学生姓名&学号模糊)
        if (StringUtils.isNotBlank(keyWord)) {
            criteria.orOperator(Criteria.where("studentNo").regex(".*?" + keyWord + ".*"),
                    Criteria.where("studentName").regex(".*?" + keyWord + ".*"));
        }

        Query query = new Query();
        query.addCriteria(criteria);
        query.with(new Sort(new Order(Direction.DESC, "dayDate")));

        long count = mongoOperations.count(query, YxyStuAttendance.class);
        List<YxyStuAttendance> list = mongoOperations.find(query, YxyStuAttendance.class);

        List<Map<String, String>> rerutnList = new ArrayList<Map<String, String>>();
        for (YxyStuAttendance ysa : list) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("index", (list.indexOf(ysa) + 1) + "");
            String dateChang = ysa.getDayDate().toString();
            map.put("date", dateChang.substring(0, 4) + "-" + dateChang.substring(4, 6) + "-" + dateChang.substring(6));
            map.put("stuName", ysa.getStudentName());
            map.put("stuNo", ysa.getStudentNo() == null ? null : ysa.getStudentNo().toString());
            map.put("className", ysa.getClassName());
            map.put("signInDate",
                    ysa.getSignInDate() == null ? null
                            : ysa.getSignInDate().getHours() + ":" + (ysa.getSignInDate().getMinutes() < 10
                                    ? "0" + ysa.getSignInDate().getMinutes() : ysa.getSignInDate().getMinutes()));
            map.put("signOutDate",
                    ysa.getSignOutDate() == null ? null
                            : ysa.getSignOutDate().getHours() + ":" + (ysa.getSignOutDate().getMinutes() < 10
                                    ? "0" + ysa.getSignOutDate().getMinutes() : ysa.getSignOutDate().getMinutes()));
            map.put("inspection", ysa.getInspection());
            map.put("inspectionDate",
                    ysa.getInspectionDate() == null ? null
                            : ysa.getInspectionDate().getHours() + ":"
                                    + (ysa.getInspectionDate().getMinutes() < 10
                                            ? "0" + ysa.getInspectionDate().getMinutes()
                                            : ysa.getInspectionDate().getMinutes()));
            map.put("signClass", ysa.getSignClass() == 0 ? "未到班" : "已到班");
            map.put("signClassDate",
                    ysa.getSignClassDate() == null ? null
                            : ysa.getSignClassDate().getHours() + ":"
                                    + (ysa.getSignClassDate().getMinutes() < 10
                                            ? "0" + ysa.getSignClassDate().getMinutes()
                                            : ysa.getSignClassDate().getMinutes()));
            rerutnList.add(map);
        }
        return rerutnList;
    }

    /**
     * 学生&家长查看记录时所需数据
     * 
     */
    @Override
    public List<Object> getStuAttendanceForStuOrPer(String schoolId, String studentId, Integer month, Integer pageNum,
            Integer pageSize) {
        Integer skip = pageNum * pageSize;
        Criteria criteria = new Criteria();
        // 学校id
        criteria.and("schoolId").is(schoolId);
        // 学生id
        criteria.and("studentId").is(studentId);
        // 月份数据
        criteria.and("monthDate").is(month);
        // 输入参数 (在学生姓名&学号模糊)

        Query query = new Query();
        query.addCriteria(criteria);
        query.with(new Sort(new Order(Direction.DESC, "dayDate")));

        long count = mongoOperations.count(query, YxyStuAttendance.class);
        query.skip(skip);
        query.limit(pageSize);
        List<YxyStuAttendance> list = mongoOperations.find(query, YxyStuAttendance.class);

        List<Object> rerutnList = new ArrayList<Object>();
        for (YxyStuAttendance ysa : list) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("index", (skip + new Integer(list.indexOf(ysa) + 1)));
            map.put("id", ysa.getId());
            String dateChang = ysa.getDayDate().toString();
            map.put("date", dateChang.substring(0, 4) + "-" + dateChang.substring(4, 6) + "-" + dateChang.substring(6));
            map.put("stuName", ysa.getStudentName());
            map.put("stuNo", ysa.getStudentNo() == null ? null : ysa.getStudentNo());
            map.put("className", ysa.getClassName());
            map.put("signInDate",
                    ysa.getSignInDate() == null ? null
                            : ysa.getSignInDate().getHours() + ":" + (ysa.getSignInDate().getMinutes() < 10
                                    ? "0" + ysa.getSignInDate().getMinutes() : ysa.getSignInDate().getMinutes()));
            map.put("signOutDate",
                    ysa.getSignOutDate() == null ? null
                            : ysa.getSignOutDate().getHours() + ":" + (ysa.getSignOutDate().getMinutes() < 10
                                    ? "0" + ysa.getSignOutDate().getMinutes() : ysa.getSignOutDate().getMinutes()));
            map.put("inspection", ysa.getInspection());
            map.put("inspectionDate",
                    ysa.getInspectionDate() == null ? null
                            : ysa.getInspectionDate().getHours() + ":"
                                    + (ysa.getInspectionDate().getMinutes() < 10
                                            ? "0" + ysa.getInspectionDate().getMinutes()
                                            : ysa.getInspectionDate().getMinutes()));
            map.put("signClass", ysa.getSignClass() == 0 ? "未到班" : "已到班");
            map.put("signClassDate",
                    ysa.getSignClassDate() == null ? null
                            : ysa.getSignClassDate().getHours() + ":"
                                    + (ysa.getSignClassDate().getMinutes() < 10
                                            ? "0" + ysa.getSignClassDate().getMinutes()
                                            : ysa.getSignClassDate().getMinutes()));
            rerutnList.add(map);
        }
        return rerutnList;
    }

    /**
     * 学生&家长查看记录时所需数据数量
     * 
     */
    @Override
    public Long getStuAttendanceForStuOrPerCount(String schoolId, String studentId, Integer month, Integer pageNum,
            Integer pageSize) {
        Integer skip = (pageNum - 1) * pageSize;
        Criteria criteria = new Criteria();
        // 学校id
        criteria.and("schoolId").is(schoolId);
        // 学生id
        criteria.and("studentId").is(studentId);
        // 月份数据
        criteria.and("monthDate").is(month);
        // 输入参数 (在学生姓名&学号模糊)

        Query query = new Query();
        query.addCriteria(criteria);
        query.with(new Sort(new Order(Direction.DESC, "dayDate")));

        long count = mongoOperations.count(query, YxyStuAttendance.class);
        return count;
    }

    /**
     * 正则判断是否为数字
     */
    public static Pattern patternInt = Pattern.compile("(^[+]?([0-9]|([1-9][0-9]*)))");

    public boolean IntegerUtil(String num) {
        if (num == null) { return false; }
        return patternInt.matcher(num).matches();
    }
}
